/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.java.dao;

import com.java.mapbean.Nguoidung;
import com.java.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author quannq
 */
public class NguoidungDAO {

    private final SessionFactory sessionFactory = getSessionFactory();

    protected SessionFactory getSessionFactory() {
        try {
            return HibernateUtil.getSessionFactory();
        } catch (Exception e) {
            throw new IllegalStateException(
                    "Could not locate SessionFactory in JNDI");
        }
    }

    protected void persist(Nguoidung transientInstance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.persist(transientInstance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void update(Nguoidung instance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.update(instance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void save(Nguoidung instance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.save(instance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected void delete(Nguoidung persistentInstance) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            session.delete(persistentInstance);
            transaction.commit();
            session.flush();
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected Nguoidung merge(Nguoidung detachedInstance, Session session) {
        try {
            Nguoidung result = (Nguoidung) session.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            throw re;
        }
    }

    protected Nguoidung findById(int id) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            Nguoidung instance = (Nguoidung) session.get(
                    "com.java.mapbean.Nguoidung", id);
            transaction.commit();
            session.flush();
            return instance;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    @SuppressWarnings("unchecked")
    protected List<Nguoidung> findAll() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Nguoidung> results = session.createQuery(
                    "from com.java.mapbean.Nguoidung").list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Nguoidung> findEmp() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Nguoidung> results = session.createQuery(
                    "from com.java.mapbean.Nguoidung n where n.quyen.maquyen <>3").list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    protected List<Nguoidung> findCus() {
        Session session = null;
        Transaction transaction = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();

            List<Nguoidung> results = session.createQuery(
                    "from com.java.mapbean.Nguoidung n where n.quyen.maquyen=3").list();

            transaction.commit();
            session.flush();
            return results;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public Nguoidung findUsername(String username) {
        Session session = null;
        Transaction transaction = null;
        Nguoidung nguoidung = null;
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            Criteria criteria = session.createCriteria(Nguoidung.class);
            criteria.add(Restrictions.eq("username", username));
            List list = criteria.list();
            if (list != null && list.size() > 0) {
                nguoidung = (Nguoidung) list.get(0);
            }
            transaction.commit();
            session.flush();
            return nguoidung;
        } catch (RuntimeException re) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw re;
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
